from pymongo import MongoClient
from urllib import parse

import requests
import random
import re
import sys
import pymongo

client = MongoClient('127.0.0.1', 27017, connect = False)

qushu_resume_db = client['qushu_resume_db']
qushu_resume_coll = qushu_resume_db['qushu_resume_coll']

i = 0
total = qushu_resume_coll.count()
folders = 10
seglen = int(total / folders)

for n in range(folders):
    print(seglen * n, seglen * (n + 1))
    resumes = []

    i = 0
    total = seglen

    for item in qushu_resume_coll.find({}, {'token': 0}).skip(seglen * i).limit(seglen):
        i += 1

        percent = float(i) * 100 / float(total)
        sys.stdout.write("%.2f" % percent);
        sys.stdout.write("\r");
        sys.stdout.flush()

        resumes.append(item)

    print(n, 'writing...')

    with open('/root/home/cv_corp_' + str(n) + '.txt', 'w', encoding = 'utf-8') as file:
        for item in resumes:
            file.write(':::' + re.sub('\n', '', item['wanted_job']['title']) + ':::\n')

            for we in item['work_experience']:
                file.write(re.sub('\n', '', we['describe']) + '\n')

            for pe in item['proj_experience']:
                file.write(re.sub('\n', '', pe['describe']) + '\n')
                file.write(re.sub('\n', '', pe['responsibility']) + '\n')

            file.write(re.sub('\n', '', item['self_introduce']) + '\n')
